Ext.define('Admin.view.user.info.UserInfoViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.userInfoViewController',

    onResetButton: function(btn) {
        var form = btn.up('form').getForm();
        form.reset();
        var data = Ext.util.JSON.decode(userInfo);
        form.setValues(data);
        form.findField('dept').setValue(data.dept);
    },

    onSubmitButton: function(btn) {
        var me = this,
            form = btn.up('form').getForm(),
            data = Ext.util.JSON.decode(userInfo),
            birthday = form.findField("birthday").getValue(),
            enterTime = form.findField("enterTime").getValue(),
            dept = form.findField("dept").getValue();

        if (dept == data.dept) {
            dept = data.deptId;
        }

        Ext.Ajax.request({
            url: 'user/updateUser',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                id: userId,
                fullname: form.findField("fullname").getValue(),
                age: form.findField("age").getValue(),
                email: form.findField("email").getValue(),
                sex: form.findField("sex").getGroupValue(),
                phone: form.findField("phone").getValue(),
                deptId: dept,
                job: form.findField("job").getValue(),
                address: form.findField("address").getValue(),
                birthday: new Date(birthday).toLocaleDateString(),
                enterTime: new Date(enterTime).toLocaleDateString()
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code === 200) {
                    window.location.reload();
                } else {
                    Ext.Msg.alert('警告', json.message);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onCoverUploadClick: function(btn) {
        Ext.create('widget.userInfoCoverFormWindow');
    },

    onCoverFormCancelButton: function(btn) {
        var win = btn.up('window');
        if (win) {
            win.close();
        }
    },

    onCoverFormSubmitButton: function(btn) {
        var form = btn.up('form'),
            cover = form.getForm().findField("cover").getValue(),
            regex = /^.*\.(jpg|png)/;

        if (cover == null) {
            return;
        }

        if (!regex.test(cover)) {
            return;
        }

        form.getForm().submit({
            url: 'user/uploadCover',
            waitMsg: '正在上传，请耐心等待....',
            params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(form, action) {
                Ext.Msg.alert('上传成功', action.result.msg, function() {
                    btn.up('window').close();
                    window.location.reload();
                });
            },
            failure: function(form, action) {
                Ext.Msg.alert('上传失败', action.result.msg);
            }
        });
    },
});